<?php	
// Week 1 Tuesday 15th August 2006 - Monday 21st August 2006
// Saturday 19th August 2006 - first games
// Scores for week1 - Wednesday 23rd August 2006

class dates {
	// Arrays are indexed by $week
	var $startDay = array();	// start day for this week
	var $startMonth = array();	// start month for this week
	var $startYear = array();	// start year for this week
	var $endDay = array();		// end day for this week
	var $endMonth = array();	// end month for this week
	var $endYear = array();		// end year for this week
	var $maxWeeks;
	
	function dates()
	{
		// Sort out week 1 from the config database
		$this->getStartDate();
		//$this->startDay[1] = STARTDAY - 8;
		//$this->startMonth[1] = STARTMONTH;
		//$this->startYear[1] = STARTYEAR;
		
		$date = mktime(0, 0, 0, $this->startMonth[1], ($this->startDay[1]+6), $this->startYear[1]);

		$this->endDay[1] = date("d", $date);
		$this->endMonth[1] = date("m", $date);
		$this->endYear[1] = date("Y", $date);

		for ($week = 2; $week <= $this->maxWeeks; $week++)
		{
			$nextWeek = mktime(0, 0, 0, $this->startMonth[$week-1], ($this->startDay[$week-1]+7), $this->startYear[$week-1]);
			$this->startDay[$week] = date("d", $nextWeek);
			$this->startMonth[$week] = date("m", $nextWeek);
			$this->startYear[$week] = date("Y", $nextWeek);
			
			$nextWeek = mktime(0, 0, 0, $this->startMonth[$week-1], ($this->startDay[$week-1]+13), $this->startYear[$week-1]);
			$this->endDay[$week] = date("d", $nextWeek);
			$this->endMonth[$week] = date("m", $nextWeek);
			$this->endYear[$week] = date("Y", $nextWeek);
			//echo "<BR>".$week."this ".date("Y-m-d H:i:s", $nextWeek);
		}
	}
	
	function getStartDateFromWeek($week)
	{
		return mktime(0, 0, 0, $this->startMonth[$week], $this->startDay[$week], $this->startYear[$week]);
	}
	
	function getEndDateFromWeek($week)
	{
		return mktime(0, 0, 0, $this->endMonth[$week], $this->endDay[$week], $this->endYear[$week]);
	}
		
	function getCurrentWeek()
	{
		$today = getdate();
		$thisWeek = mktime(0, 0, 0, $today['mon'], $today['mday'], $today['year']);
	
		for ($week = 1; $week <= $this->maxWeeks; $week++)
		{
			$currentWeek = mktime(0, 0, 0, $this->startMonth[$week], $this->startDay[$week], $this->startYear[$week]);
			if (date("Y-m-d H:i:s", $thisWeek) < date("Y-m-d H:i:s",$currentWeek))
			{
				//echo "<BR>This week = ".$week." this date ".date("Y-m-d H:i:s", $thisWeek). " next date ".date("Y-m-d H:i:s", $currentWeek);
				return ($week - 1);
			}
		}
		//echo "<BR>ERROR finding current week". $this->maxWeeks;
		return $this->maxWeeks;
	}

	function getStartDate()
	{
		// Get the start data from the config database
		$cDatabase = new database();

		$connect = $cDatabase->connectToDatabase();
		$activeDatabase = $cDatabase->getActiveDatabase($connect);
	
		$db = $cDatabase->selectDatabase($connect, $activeDatabase);

		$selectArray[0] = '*';
		$result = $cDatabase->selectFromDatabase($connect, 'config', $selectArray, NULL, NULL, NULL, NULL);
		
		$finfo = mysqli_fetch_fields($result);
		$row = mysqli_fetch_array($result, MYSQLI_ASSOC);

		$this->startDay[1] = $row['startDay'] - 4;	// Take off 4 days to get to Tuesday which is the start of the actual week
		$this->startMonth[1] = $row['startMonth'];
		$this->startYear[1] = $row['startYear'];
		
		$this->maxWeeks = $row['maxWeeks'];
    
    $this->maxWeeks = $cDatabase->GetMaxWeeks($connect);
	}

	function getStartMonth()
	{

		return $this->startMonth[1];
	}

	function getEndMonth()
	{

		return $this->startMonth[$this->maxWeeks];
	}

	function getStartDateForMonth($month)
	{
		if ($month < 8)
			$thisWeek = mktime(0, 0, 0, $month, 1, ($this->startYear[1]+1));	// Will be the 1st of the selected month
		else
			$thisWeek = mktime(0, 0, 0, $month, 1, $this->startYear[1]);	// Will be the 1st of the selected month

		// Find out the first start date for a week in the month
		for ($week = 1; $week <= $this->maxWeeks; $week++)
		{
			$currentWeek = mktime(0, 0, 0, $this->startMonth[$week], $this->startDay[$week], $this->startYear[$week]);
			if (date("Y-m-d H:i:s", $thisWeek) <= date("Y-m-d H:i:s",$currentWeek))
			{
				// We've found the first week that is greater than 1st of our month
				//echo "<BR>start This week = ".$week." this date ".date("Y-m-d H:i:s", $thisWeek). " next date ".date("Y-m-d H:i:s", $currentWeek);
				return ($currentWeek);
			}
		}
	}

	function getEndDateForMonth($month)
	{
		// Use mktime to create a timestamp one month into the future, but one
		// day less.  Also make the time for almost midnight, so it can be
		// used as an 'end of month' boundary
		if ($month < 8)
			$dateStamp = mktime(23, 59, 59, $month + 1, 0, ($this->startYear[1]+1));
		else
			$dateStamp = mktime(23, 59, 59, $month + 1, 0, $this->startYear[1]);

		
		$lastDay = date('d', $dateStamp);
		if ($month < 8)
			$thisWeek = mktime(0, 0, 0, $month, $lastDay, ($this->startYear[1]+1));	// Will be the 1st of the selected month
		else
			$thisWeek = mktime(0, 0, 0, $month, $lastDay, $this->startYear[1]);	// Will be the 1st of the selected month

		// Find out the last date for this month
		for ($week = 1; $week <= $this->maxWeeks; $week++)
		{	
			// Add on 6 days to get to the end date for this week
			$currentWeek = mktime(0, 0, 0, $this->startMonth[$week], ($this->startDay[$week]+6), $this->startYear[$week]);

			if (date("Y-m-d H:i:s", $thisWeek) <= date("Y-m-d H:i:s",$currentWeek))
			{
				// We've found the first week that is greater than 1st of our month
				//echo "<BR>end This week = ".$week." this date ".date("Y-m-d H:i:s", $thisWeek). " next date ".date("Y-m-d H:i:s", $currentWeek);

				return ($currentWeek);
			}
		}

		// We've dropped out without returning so this must be the last month, return the end date for the last week
		$endWeek = mktime(0, 0, 0, $this->startMonth[$this->maxWeeks], ($this->startDay[$this->maxWeeks]+6), $this->startYear[$this->maxWeeks]);

		return ($endWeek);
	}

	function getWeekFromDate($date, $flag)
	{		
		for ($week = 1; $week <= $this->maxWeeks; $week++)
		{
			if ($flag == 'start')
			{
				if ((date("d",$date) == $this->startDay[$week]) && (date("m",$date) == $this->startMonth[$week]) && (date("Y",$date) == $this->startYear[$week]))
				{
					return $week;
				}
			}
			else
			{
				if ((date("d",$date) == $this->endDay[$week]) && (date("m",$date) == $this->endMonth[$week]) && (date("Y",$date) == $this->endYear[$week]))
				{
					return $week;
				}
			}
		}
	}

	function getMonthFromWeek($week)
	{
		return $this->startMonth[$week];
	}

	function getMonthName($month)
	{
		switch ($month)
		{
			case 1	:	$monthName = 'January';
						break;
			case 2	:	$monthName = 'February';
						break;
			case 3	:	$monthName = 'March';
						break;
			case 4	:	$monthName = 'April';
						break;
			case 5	:	$monthName = 'May';
						break;
			case 6	:	$monthName = 'June';
						break;
			case 7	:	$monthName = 'July';
						break;
			case 8	:	$monthName = 'August';
						break;
			case 9	:	$monthName = 'September';
						break;
			case 10	:	$monthName = 'October';
						break;
			case 11	:	$monthName = 'November';
						break;
			case 12	:	$monthName = 'December';
						break;
			default	:	$monthName = 'Unknown';
						break;
		}
		return $monthName;
	}

	function getMonthNumber($month)
	{
		switch ($month)
		{
			case 'January'	:	$monthName = 1;
								break;
			case 'February'	:	$monthName = 2;
								break;
			case 'March'	:	$monthName = 3;
								break;
			case 'April'	:	$monthName = 4;
								break;
			case 'May'	:		$monthName = 5;
								break;
			case 'June'	:		$monthName = 6;
								break;
			case 'July'	:		$monthName = 7;
								break;
			case 'August'	:	$monthName = 8;
								break;
			case 'September':	$monthName = 9;
								break;
			case 'October'	:	$monthName = 10;
								break;
			case 'November'	:	$monthName = 11;
								break;
			case 'December'	:	$monthName = 12;
								break;
			default			:	$monthName = 8;
								break;
		}
		return $monthName;
	}
	
	function getToday()
	{
		$today = getdate();
		return $today['weekday'];
	}
}

?>